package net.shopxx.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.shopxx.dao.MenuDao;
import net.shopxx.entity.Menu;
import net.shopxx.service.MenuService;

import org.hibernate.Hibernate;
import org.springframework.stereotype.Service;
import org.springmodules.cache.annotations.CacheFlush;
import org.springmodules.cache.annotations.Cacheable;

/**
 * Service实现类 - 菜单
 */

@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu, String> implements MenuService {

	@Resource
	private MenuDao menuDao;
	
	@Resource
	public void setBaseDao(MenuDao menuDao) {
		super.setBaseDao(menuDao);
	}

	@Cacheable(modelId = "caching")
	public List<Menu> getRootMenuList() {
		List<Menu> rootMenuList = menuDao.getRootMenuList();
		if (rootMenuList != null) {
			for (Menu rootMenu : rootMenuList) {
				Hibernate.initialize(rootMenu);
			}
		}
		return rootMenuList;
	}
	
	@Cacheable(modelId = "caching")
	public List<Menu> getParentMenuList(Menu menu) {
		List<Menu> parentMenuList = menuDao.getParentMenuList(menu);
		if (parentMenuList != null) {
			for (Menu parentMenu : parentMenuList) {
				Hibernate.initialize(parentMenu);
			}
		}
		return parentMenuList;
	}
	

	
	public List<Menu> getMenuPathList(Menu menu) {
		List<Menu> menuPathList = new ArrayList<Menu>();
		menuPathList.addAll(this.getParentMenuList(menu));
		menuPathList.add(menu);
		return menuPathList;
	}

	
	@Cacheable(modelId = "caching")
	public List<Menu> getChildrenMenuList(Menu menu) {
		List<Menu> childrenMenuList = menuDao.getChildrenMenuList(menu);
		if (childrenMenuList != null) {
			for (Menu childrenMenu : childrenMenuList) {
				Hibernate.initialize(childrenMenu);
			}
		}
		return childrenMenuList;
	}
	

	
	@Cacheable(modelId = "caching")
	public List<Menu> getMenuTreeList() {
		List<Menu> allMenuList = this.getAll();
		System.out.println(allMenuList.size());
		return recursivMenuTreeList(allMenuList, null, null);
	}
	
	// 递归父类排序分类树
	private List<Menu> recursivMenuTreeList(List<Menu> allMenuList, Menu p, List<Menu> temp) {
		if (temp == null) {
			temp = new ArrayList<Menu>();
		}
		for (Menu menu : allMenuList) {
			Menu parent = menu.getParent();
			if ((p == null && parent == null) || (menu != null && parent == p)) {
				temp.add(menu);
				if (menu.getChildren() != null && menu.getChildren().size() > 0) {
					recursivMenuTreeList(allMenuList, menu, temp);
				}
			}
		}
		return temp;
	}

	@Override
	@Cacheable(modelId = "caching")
	public List<Menu> getAll() {
		List<Menu> allMenu = menuDao.getAll();
		if (allMenu != null) {
			for (Menu menu : allMenu) {
				Hibernate.initialize(menu);
			}
		}
		return allMenu;
	}

	@Override
	@CacheFlush(modelId = "flushing")
	public void delete(Menu menu) {
		menuDao.delete(menu);
	}

	@Override
	@CacheFlush(modelId = "flushing")
	public void delete(String id) {
		menuDao.delete(id);
	}

	@Override
	@CacheFlush(modelId = "flushing")
	public void delete(String[] ids) {
		menuDao.delete(ids);
	}

	@Override
	@CacheFlush(modelId = "flushing")
	public String save(Menu menu) {
		return menuDao.save(menu);
	}

	@Override
	@CacheFlush(modelId = "flushing")
	public void update(Menu menu) {
		menuDao.update(menu);
	}
}